package birdry.effects
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import com.greensock.*;
    import transition.events.*;
    import transition.utils.*;

    public class FreeTransformEffect extends EventDispatcher
    {
        private var _container:DisplayObjectContainer;
        private var _prop:Object;
        private var _distort:DistortImage;
        private var _dis:Sprite;
        private var _delay:Number = 0.01;
        private var _targetProp:Object;
        private var _onComplete:Function;
        private var _isPlaying:Boolean;
        private var _onUpdate:Function;
        private var _h:int;
        private var _onInit:Function;
        private var _w:int;
        private var _x:int;
        private var _y:int;
        private var _segment:int;
        private var _sp:Sprite;

        public function FreeTransformEffect(param1:DisplayObjectContainer = null, param2:DisplayObject = null, param3:int = 4, param4:Object = null)
        {
            this._sp = new Sprite();
            this._container = param1;
            this.emptyClip(this._container);
            this._segment = param3;
            if (param4)
            {
                this._prop = param4;
                if (this._prop.delay)
                {
                    this._delay = this._prop.delay;
                }
            }
            if (this._container != null)
            {
                param1.addChild(this._sp);
            }
            return;
        }// end function

        public function fromTo(param1:Sprite, param2:Number, param3:Rectangle, param4:Rectangle, param5:Function = null) : void
        {
            var _loc_20:* = param1;
            this._dis = param1;
            var _loc_20:* = new DistortImage(param1.width, param1.height, this._segment, this._segment);
            this._distort = new DistortImage(param1.width, param1.height, this._segment, this._segment);
            this._distort.setSize(param1.width, param1.height);
            var _loc_6:* = BitmapU.convertBitmapData(this._dis);
            var _loc_7:* = param4;
            var _loc_8:* = param3;
            var _loc_9:* = new Point(_loc_8.x, _loc_8.y);
            var _loc_10:* = new Point(_loc_8.x + _loc_8.width, _loc_8.y);
            var _loc_11:* = new Point(_loc_8.x + _loc_8.width, _loc_8.y + _loc_8.height);
            var _loc_12:* = new Point(_loc_8.x, _loc_8.y + _loc_8.height);
            var _loc_13:* = new Point(_loc_7.x, _loc_7.y);
            var _loc_14:* = new Point(_loc_7.x + _loc_7.width, _loc_7.y);
            var _loc_15:* = new Point(_loc_7.x + _loc_7.width, _loc_7.y + _loc_7.height);
            var _loc_16:* = new Point(_loc_7.x, _loc_7.y + _loc_7.height);
            var _loc_17:Array = [Point.distance(_loc_9, _loc_13), Point.distance(_loc_10, _loc_14), Point.distance(_loc_11, _loc_15), Point.distance(_loc_12, _loc_16)];
            var _loc_18:Array = [TweenLite.to(_loc_9, param2, {delay:_loc_17[0] / _loc_17[2] * this._delay, x:_loc_13.x, y:_loc_13.y, ease:param5}), TweenLite.to(_loc_10, param2, {delay:_loc_17[1] / _loc_17[3] * this._delay, x:_loc_14.x, y:_loc_14.y, ease:param5}), TweenLite.to(_loc_11, param2, {delay:_loc_17[2] / _loc_17[0] * this._delay, x:_loc_15.x, y:_loc_15.y, ease:param5}), TweenLite.to(_loc_12, param2, {delay:_loc_17[3] / _loc_17[1] * this._delay, x:_loc_16.x, y:_loc_16.y, ease:param5})];
            var _loc_20:* = _loc_7;
            this._targetProp = _loc_7;
            var _loc_19:* = new TimelineLite({paused:true, tweens:_loc_18, align:TweenAlign.NORMAL, onUpdate:this.onUpdate, onUpdateParams:[_loc_6, _loc_9, _loc_10, _loc_11, _loc_12], onComplete:this.onComplete});
            new TimelineLite({paused:true, tweens:_loc_18, align:TweenAlign.NORMAL, onUpdate:this.onUpdate, onUpdateParams:[_loc_6, _loc_9, _loc_10, _loc_11, _loc_12], onComplete:this.onComplete}).play();
            trace("TOTAL_TIMELINE::", _loc_19.duration);
            if (this._prop)
            {
                TweenMax.to(this._container, _loc_19.duration, this._prop);
            }
            return;
        }// end function

        public function onComplete() : void
        {
            this._sp.graphics.clear();
            this._container.addChild(this._dis);
            var _loc_1:* = this._targetProp.x;
            this._dis.x = this._targetProp.x;
            var _loc_1:* = this._targetProp.y;
            this._dis.y = this._targetProp.y;
            var _loc_1:* = this._targetProp.width;
            this._dis.width = this._targetProp.width;
            var _loc_1:* = this._targetProp.height;
            this._dis.height = this._targetProp.height;
            var _loc_1:String = null;
            this._prop = null;
            dispatchEvent(new JEvent(JEvent.COMPLETE));
            return;
        }// end function

        public function onInit() : void
        {
            return;
        }// end function

        public function to(param1:Sprite, param2:Number, param3:Rectangle, param4:Function = null) : void
        {
            var _loc_5:* = param1;
            var _loc_18:* = new DistortImage(param1.width, param1.height, this._segment, this._segment);
            this._distort = new DistortImage(param1.width, param1.height, this._segment, this._segment);
            this._distort.setSize(param1.width, param1.height);
            var _loc_6:* = BitmapU.convertBitmapData(_loc_5);
            var _loc_7:* = param3;
            var _loc_8:* = new Point(_loc_5.x, _loc_5.y);
            var _loc_9:* = new Point(_loc_5.x + _loc_5.width, _loc_5.y);
            var _loc_10:* = new Point(_loc_5.x + _loc_5.width, _loc_5.y + _loc_5.height);
            var _loc_11:* = new Point(_loc_5.x, _loc_5.y + _loc_5.height);
            var _loc_12:* = new Point(_loc_7.x, _loc_7.y);
            var _loc_13:* = new Point(_loc_7.x + _loc_7.width, _loc_7.y);
            var _loc_14:* = new Point(_loc_7.x + _loc_7.width, _loc_7.y + _loc_7.height);
            var _loc_15:* = new Point(_loc_7.x, _loc_7.y + _loc_7.height);
            var _loc_16:Array = [TweenLite.to(_loc_8, param2 * Math.random(), {delay:Math.random() * 0.1, x:_loc_12.x, y:_loc_12.y, ease:param4}), TweenLite.to(_loc_9, param2 * Math.random(), {delay:Math.random() * 0.2, x:_loc_13.x, y:_loc_13.y, ease:param4}), TweenLite.to(_loc_10, param2 * Math.random(), {delay:Math.random() * 0.3, x:_loc_14.x, y:_loc_14.y, ease:param4}), TweenLite.to(_loc_11, param2 * Math.random(), {delay:Math.random() * 0.4, x:_loc_15.x, y:_loc_15.y, ease:param4})];
            var _loc_17:* = new TimelineLite({paused:true, tweens:_loc_16, align:TweenAlign.NORMAL, onUpdate:this.onUpdate, onUpdateParams:[_loc_6, _loc_8, _loc_9, _loc_10, _loc_11], onComplete:this.onComplete});
            new TimelineLite({paused:true, tweens:_loc_16, align:TweenAlign.NORMAL, onUpdate:this.onUpdate, onUpdateParams:[_loc_6, _loc_8, _loc_9, _loc_10, _loc_11], onComplete:this.onComplete}).play();
            if (this._prop)
            {
                TweenMax.to(this._container, _loc_17.duration, this._prop);
            }
            return;
        }// end function

        private function onUpdate(param1:BitmapData, param2:Point, param3:Point, param4:Point, param5:Point) : void
        {
            this._sp.graphics.clear();
            this._distort.setTransform(this._sp.graphics, param1, param2, param3, param4, param5);
            dispatchEvent(new JEvent(JEvent.UPDATE));
            return;
        }// end function

        private function propParser() : void
        {
            var _loc_1:String = null;
            for (_loc_1 in this._prop)
            {
                
            }
            return;
        }// end function

        public function kill() : void
        {
            return;
        }// end function

        public function draw() : void
        {
            return;
        }// end function

        private function emptyClip(param1:DisplayObjectContainer) : void
        {
            var _loc_2:int = 0;
            var _loc_3:* = param1.numChildren;
            _loc_2 = _loc_3;
            while (_loc_2 > 0)
            {
                
                param1.removeChildAt(0);
                _loc_2 = _loc_2 - 1;
            }
            return;
        }// end function

    }
}
